import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Player implements IPlayer, Runnable {

	public static final int RECT_WIDTH = 40;
	public static final int RECT_HEIGHT = 170;
	private int posX = 50;
	private int posY = 90;
	private int yDir;	// valore usato solo dal COM 
	private ImageIcon i;
	private Image image;
	protected Option gameOption;
	private int number;
	
	private boolean moveUp;
	private boolean moveDown;

	public Player(Option gameOption, int n_Player) {
		this.gameOption = gameOption;
		number = n_Player;

		// Setting the Image Icon
		i = new ImageIcon("./images/Player.png");
		image = i.getImage();

		// SEtting Position
		if (number == 1)
			this.setPos(20, 200); // Giocatore uno
		else { // GIOCATORE DUE/COM
			int temp = gameOption.getMenu().getWidth() - Player.RECT_WIDTH - 20;
			this.setPos(temp, 200);
		}
	} // COSTRUTTORE

	public int getNumberPlayer() {
		return number;
	}

	public int getPosX() {
		return posX;
	}

	public int getPosY() {
		return posY;
	}

	public void setPos(int x, int y) {
		posX = x;
		posY = y;
	}
	
	public void setYDir(int pYDir) {
		yDir = pYDir;
	}
	
	public int getYDir() {
		return yDir;
	}

	public void move() {
		if (moveUp && posY > 0) {
			posY--;
		} else if (!moveUp && moveDown && posY < gameOption.getGameObjectPanel().getParentFrame().getContentPane().getHeight() - RECT_HEIGHT)	//potrebbe capitare che moveUp e moveDown siano entrambi true?
			posY++;
	}
	
	public void setMoveUp(boolean pMoveUp) {
		moveUp = pMoveUp;
	}
	
	public void setMoveDown(boolean pMoveDown) {
		moveDown = pMoveDown;
	}
	
	public boolean getMoveUp() {
		return moveUp;
	}
	
	public boolean getMoveDown() {
		return moveDown;
	}

	public Image getImage() {
		return image;
	}

	public void run() {
		try {
			while (gameOption.isRunning()) {
				synchronized (gameOption) {
					while (!gameOption.isNotInPause())
						gameOption.wait();
				}
				move();
				gameOption.getGameObjectPanel().repaint();

				Thread.sleep(7);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
